#!/usr/bin/python3
'''
Created on Mar 8, 2016

@author: grind
'''

import urllib.request
import urllib.response
import urllib.error
import time

def opensina(code):
    request = urllib.request.urlopen("http://hq.sinajs.cn/list=" + code)
    html = request.read()
    details = html.decode("gbk")
    array = details.split("=")
    #print(array[1])
    item = array[1].split(",")
    volume = float(item[9])
    offset1 = float(item[3]) - float(item[4])
    offset2 = float(item[3]) - float(item[5])
    return '[%s] %s(%.2f,%.2f), [%s,%s] vol:%d' % (item[-2], item[3], offset2, offset1, item[4], item[5], volume/10000)

if __name__ == '__main__':
    lastvol1 = 0
    lastvol2 = 0
    while True:
        result  = opensina("sh000001")
        print(result, end=' || ')
        #result2 = opensina("sz399001")
        result2 = opensina("sz399006")
        print(result2)
        #break
        time.sleep(30) # sleep 2 s
        